﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CHPM.Entities;

namespace CHPM.BL.Managers
{
    public class PatientManager : IPatientManager
    {
        #region Implementation of IPatientManager

        public List<PATIENT> GetPatients()
        {
            using (var context = new ChildHouseDBEntities())
            {
                return context.CreateObjectSet<PATIENT>().ToList();
            }
        }

        public PATIENT GetPatientById(int id)
        {
            using (var context = new ChildHouseDBEntities())
            {
                return context.PATIENTs.FirstOrDefault(p => p.Id == id);
            }
        }

        public List<PATIENT> GetUsersMinMax(int startIndex, int maxCount)
        {
            using (var conext = new ChildHouseDBEntities())
            {
                return conext.PATIENTs.Skip(startIndex).Take(maxCount).ToList();
            }
        }

        public int GetPatientCount()
        {
            using (var context = new ChildHouseDBEntities())
            {
                return context.PATIENTs.Count();
            }
        }

        public void DeletePatient(PATIENT deletedPatient)
        {
            using (var context = new ChildHouseDBEntities())
            {
                context.DeleteObject(deletedPatient);
                context.SaveChanges();
            }
        }

        public PATIENT EditPatient(PATIENT newPatient)
        {
            using (var context = new ChildHouseDBEntities())
            {
                context.AttachTo(newPatient.GetType().Name, newPatient);
                context.ObjectStateManager.ChangeObjectState(newPatient, EntityState.Modified);
                context.SaveChanges();
                return context.PATIENTs.FirstOrDefault(p => p.Id == newPatient.Id);
            }
        }

        public PATIENT InsertPatient(PATIENT newPatient)
        {
            using (var context = new ChildHouseDBEntities())
            {
                context.PATIENTs.AddObject(newPatient);
                context.SaveChanges();
                return context.PATIENTs.FirstOrDefault(p => p.Id == newPatient.Id);
            }
        }

        #endregion
    }
}
